package net.lzzy.practices.entities;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author lzzy_cyp on 2022/5/6
 * Description {id:""}
 */
@Entity
@Getter
@Setter
public class User extends BaseEntity{
    /**
     * -1：禁止
     * 0：管理
     * 1：老师
     * 2：学生
     */
    @Transient
    public static final int TYPE_BANNED=-1;
    @Transient
    public static final int TYPE_ADMIN=0;
    @Transient
    public static final int TYPE_TEACHER=1;
    @Transient
    public static final int TYPE_STUDENT=2;
    private String nickName;
    private String userName;
    private String email;
    private String phone;
    private String password;
    private String avatar;
    private int type;
    private String salt;
    private boolean applyTeacher;
    @Temporal(TemporalType.TIMESTAMP)
    private Date createTime;
    @Temporal(TemporalType.TIMESTAMP)
    private Date updateTime;
    @OneToMany(mappedBy = "teacher")
    private List<Course> courses=new ArrayList<>();
    @ManyToMany(mappedBy = "students")
    private List<Course> enrolledCourses=new ArrayList<>();
    @OneToMany(mappedBy = "student")
    private List<Result> results=new ArrayList<>();
    @OneToMany(mappedBy = "student")
    private List<Applicant> applicants=new ArrayList<>();

    public User(){
        createTime=new Date();
        updateTime=createTime;
    }

    @JsonManagedReference
    public List<Course> getCourses() {
        return courses;
    }

    @JsonIgnore
    public List<Course> getEnrolledCourses(){
        return enrolledCourses;
    }

    @JsonManagedReference
    public List<Result> getResults() {
        return results;
    }

    @JsonManagedReference
    public List<Applicant> getApplicants() {
        return applicants;
    }

    public void setEnrolledCourses(List<Course> enrolledCourses) {
        this.enrolledCourses = enrolledCourses;
    }

    public void setResults(List<Result> results) {
        this.results = results;
    }

    public void setApplicants(List<Applicant> applicants) {
        this.applicants = applicants;
    }

    public void setCourses(List<Course> courses) {
        this.courses = courses;
    }

    @Override
    public boolean equals(Object obj){
        if(this==obj){
            return true;
        }
        if(obj==null||obj.getClass() !=getClass()){
            return false;
        }
        User user=(User) obj;
        return id.equals(user.getId());
    }

    @Override
    public int hashCode(){
        return Objects.hash(id);
    }
}
